package dev.pda.display.extension

import android.util.Log
import dev.pda.display.data.common.exceptions.HttpException
import dev.pda.display.presentation.common.CustomErrorConsumer
import io.reactivex.Completable
import io.reactivex.Flowable
import io.reactivex.Single
import io.reactivex.disposables.Disposable
import io.reactivex.functions.Action
import io.reactivex.functions.Consumer

fun <T> Single<T>.customSubscribe(
    success: (T) -> Unit,
    error: (HttpException) -> Unit
): Disposable {
    return this.subscribe(Consumer {
        success(it)
    }, object : CustomErrorConsumer() {
        override fun accept(it: HttpException) {
            error(it)
        }
    })
}

fun <T> Single<T>.customSubscribe(success: (T) -> Unit): Disposable {
    return this.subscribe(Consumer {
        success(it)
    }, object : CustomErrorConsumer() {
        override fun accept(it: HttpException) {
            Log.d("CustomErrorConsumer", it.toString())
        }
    })
}

fun <T> Flowable<T>.customSubscribe(
    success: (T) -> Unit,
    error: (HttpException) -> Unit
): Disposable {
    return this.subscribe(Consumer {
        success(it)
    }, object : CustomErrorConsumer() {
        override fun accept(it: HttpException) {
            error(it)
        }
    })
}

fun Completable.customSubscribe(error: (HttpException) -> Unit): Disposable {
    return this.subscribe(Action {}, object : CustomErrorConsumer() {
        override fun accept(it: HttpException) {
            error(it)
        }
    })
}